import 'dart:async';
import 'dart:convert';
import 'dart:math';

import 'package:flutter/material.dart';
import 'sugar.dart';

///
/// ### 颜色转换函数
///
/// 支持以下格式
///
/// * 正常的6位数数值字符串 如：
/// ```dart
/// col('000000') 等价于 Color(0xff000000)
/// ```
///
/// * 3位数数值字符串 如：
/// ```dart
/// col('369') 等价于 Color(0xff336699)
/// ```
///
/// * 2位数数值字符串 如：
/// ```dart
/// col('01') 等价于 Color(0xff010101)
/// ```
///
/// * 1位数数值字符串 如：
/// ```dart
/// col('0') 等价于 Color(0xff000000)
/// ```
///
/// 注意：
///
/// 不合规则的 比如4位 1234 时 直接按Color(0xff1234) 返回
///
/// 转换失败时统一返回0xFF000000
///
Color col(String cStr) {
  String resStr;
  int def = 0xFF000000;

  if (cStr == null) return Color(def);

  switch (cStr.length) {
    case 3:
      resStr = cStr[0] * 2 + cStr[1] * 2 + cStr[2] * 2;
      break;
    case 2:
      resStr = (cStr[0] + cStr[1]) * 3;
      break;
    case 1:
      resStr = cStr * 6;
      break;
    default:
      resStr = cStr;
      break;
  }

  return Color(int.parse(resStr, radix: 16) | 0xFF000000);
}

/// 快捷px 以设置为准
double px(double px) {
  return Sugar.mediaQuery.size.width / 750 * px;
}

/// h5相关的的高度百分比
double vh(double vh) {
  return vh * Sugar.mediaQuery.size.height / 100;
}

/// h5相关的的宽度百分比
double vw(double vw) {
  return vw * Sugar.mediaQuery.size.width / 100;
}

///
/// 判断空
///
/// 当传入的值为以下值时返回假
///
/// null false '' 0 [] {}
///
bool empty(obj) {
  if (obj == null || obj == false || obj == '' || obj == 0) {
    return true;
  }
  if (obj is List && obj.length == 0) {
    return true;
  }
  if (obj is Map && obj.keys.length == 0) {
    return true;
  }
  return false;
}

/// 判断当前环境
bool get isProduct => const bool.fromEnvironment("dart.vm.product");

/// 正确的 ... 省略包括emoji的文本
String maxLength(String str, int len) {
  // 删除emoji表情
  var sRunes = str.runes;
  return sRunes.length > len
      ? String.fromCharCodes(sRunes, 0, sRunes.length - len) + "..."
      : str;
}

/// 节流
/// ```
/// callback 回调函数
/// duration 节流间隔 默认500毫秒
/// ```
Function([dynamic]) throttle(
  Function(dynamic) callback, {
  Duration duration = const Duration(milliseconds: 500),
}) {
  bool canRun = true;
  return ([agrs]) {
    if (canRun) {
      callback(agrs);
      Timer(duration, () => canRun = true);
      canRun = false;
    }
  };
}

/// 手动防抖
/// ```
/// callback(reset, agrs) 回调函数
/// reset 重置状态的方法
/// agrs 可能传过来的参数
/// ```
Function([dynamic]) debounce(Function(Function(), dynamic) callback) {
  bool canRun = true;

  // 恢复可执行状态
  reset() => canRun = true;

  return ([agrs]) {
    if (canRun) {
      canRun = false;
      callback(reset, agrs);
    }
  };
}

/// 根据时间戳生成随机字符串
String randomKeyByTimestamp([int strlenght = 6]) {
  final alphabet =
      'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890';
  final timestamp = DateTime.now().microsecondsSinceEpoch;
  String left = '';
  for (var i = 0; i < strlenght; i++) {
    left = left + alphabet[Random().nextInt(alphabet.length)];
  }
  return '$timestamp$left';
}

///
/// 判断是否json字符串
///
bool isJson(str) {
  try {
    json.decode(str);
    return true;
  } catch (e) {
    return false;
  }
}
